@extends('base')

@section('style')
<style>
    /*权限*/
    .permission-list{ border:solid 1px #eee; padding-bottom: 10px}
    .permission-list > dt{ background-color:#efefef;padding:5px 10px}
    .permission-list > dd{ padding:0 10px; padding-left:30px}
    .permission-list > dd > dl{ border-bottom:solid 1px #eee; padding:5px 0}
    .permission-list > dd > dl > dt{ display:inline-block;white-space:nowrap;}
    .permission-list > dd > dl > dd{ display:inline-block;margin-left:20px;}
    .permission-list > dd > dl > dd >label{ margin-left:10px;}
</style>
@endsection

@section('content')
<form class="layui-form" name="edit_form" lay-filter="edit_form">
    <div class="mainbox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="role_name" value="{{ $data['role_name'] }}" lay-verify="required" placeholder="请输入角色名称" class="layui-input">
                    <input type="hidden" name="id" value="{{ $data['id'] }}">
                </div>
                <label class="layui-form-label">角色描述：</label>
                <div class="layui-input-inline">
                    <input type="text" name="role_intro" value="{{ $data['role_intro'] }}" lay-verify="required" placeholder="请输入角色描述" class="layui-input">
                </div>
                <label class="layui-form-label">排序值：</label>
                <div class="layui-input-inline">
                    <input type="number" name="role_sort" value="{{ $data['role_sort'] }}" lay-verify="required|number" placeholder="请输入排序值" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色权限：</label>
                <div class="layui-input-block">
                    @if ($data['id'] === 1)
                    <div class="layui-form-mid layui-word-aux"><span>本角色默认拥有所有权限</span></div>
                    @else
                    @foreach($menu as $key=>$vo)
                    <dl class="permission-list">
                        <dt>
                            <label><input lay-ignore type="checkbox" value="{{ $vo['id'] }}" name="auth" id="permission-list-{{ $key }}" @if( in_array($vo['id'], $data['menus'])) checked @endif> {{ $vo['menu_name'] }}</label>
                        </dt>
                        @foreach($vo['child'] as $k=>$v)
                        <dd>
                            <dl class="permission-list2">
                                <dt>
                                    <label><input lay-ignore type="checkbox" value="{{ $v['id'] }}" name="auth" id="permission-list-{{ $key }}-{{ $k }}" @if( in_array($v['id'], $data['menus'])) checked @endif> {{ $v['menu_name'] }}</label>
                                </dt>
                                <dd>
                                    @foreach($v['child'] as $kk=>$vv)
                                    <label><input lay-ignore type="checkbox" value="{{ $vv['id'] }}" name="auth" id="permission-list-{{ $key }}-{{ $k }}-{{ $kk }}" @if( in_array($vv['id'], $data['menus'])) checked @endif> {{ $vv['menu_name'] }}</label>
                                    @endforeach
                                </dd>
                            </dl>
                        </dd>
                        @endforeach
                    </dl>@endforeach
                    @endif
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit lay-filter="edit-save"><i class="layui-icon layui-icon-ok"></i>提交</button>
            <button type="reset" class="pear-btn pear-btn-sm"><i class="layui-icon layui-icon-refresh"></i>重置</button>
        </div>
    </div>
</form>
@endsection

@section('js')
<script>
    layui.use(['form', 'jquery'], function() {
        let form = layui.form;
        let $ = layui.jquery;

        $(".permission-list dt input:checkbox").click(function(){
            $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
        });

        $(".permission-list2 input:checkbox").click(function(){
            var l =$(this).parent().parent().find("input:checked").length;
            var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if($(this).prop("checked")){
                $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
            } else{
                if(l == 0){
                    $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
                }
                if(l2 == 0){
                    $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
                }
            }
        });

        form.on('submit(edit-save)', function(data) {
            let checkID=[];
            $("input[name='auth']:checked").each(function(i){
                checkID[i] = $(this).val();
            });
            data.field.auth = checkID;
            $.ajax({
                url: '/role/edit',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(result) {
                    if (result.status) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.layui.table.reload("role-table");
                        });
                    } else {layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
@endsection
